package com.syp.auth.dao.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.syp.auth.dao.po.ResourceEntity;

@Repository
public interface ResourceRepository extends JpaRepository<ResourceEntity, Long>, JpaSpecificationExecutor<ResourceEntity> {

	/**
	 * 根据上级资源ID删除资源
	 * 
	 * @param resPid
	 * @return
	 */
	@Query("FROM ResourceEntity WHERE resPid=:resPid")
	@Modifying
	public int deleteByPid(@Param("resPid") Long resPid);

	/**
	 * 更新上级资源ID
	 * 
	 * @param oldPid
	 * @param newPid
	 * @return
	 */
	@Query("UPDATE ResourceEntity SET resPid=:newPid WHERE resPid=:oldPid")
	@Modifying
	public int updatePidByPid(Long oldPid, Long newPid);

	/**
	 * 根据资源编码查询资源
	 * 
	 * @param resCode
	 * @return
	 */
	public ResourceEntity getByResCode(String resCode);

	/**
	 * 根据应用ID查询资源列表
	 * 
	 * @param appCode
	 * @return
	 */
	@Query("SELECT COUNT(*) FROM ResourceEntity WHERE appId=:appId")
	public long countByAppId(@Param("appId") Long appId);

	/**
	 * 根据应用编码查询资源列表
	 * 
	 * @param appCode
	 * @return
	 */
	@Query("SELECT COUNT(*) FROM ResourceEntity WHERE appCode=:appCode")
	public long countByAppCode(@Param("appCode") String appCode);

	/**
	 * 根据应用编码查询资源列表
	 * 
	 * @param appCode
	 * @return
	 */
	@Query("FROM ResourceEntity WHERE appCode=:appCode ORDER BY resSeq,id")
	public List<ResourceEntity> getByAppCode(@Param("appCode") String appCode);

	/**
	 * 根据应用编码查询受限资源列表
	 * 
	 * @param appCode
	 * @param resIdList
	 * @return
	 */
	@Query("FROM ResourceEntity WHERE appCode=:appCode AND id in(:resIdList) ORDER BY resSeq,id")
	public List<ResourceEntity> getByAppCodeLimited(@Param("appCode") String appCode, @Param("resIdList") List<Long> resIdList);
}